package com.pengtu.gsj.service.impl.check.yh.technical;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.yh.TecConEvalution;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.DownService;
import com.pengtu.gsj.service.check.yh.technical.AutomationEquipmentService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.service.yh.TecconevalutionService;
import com.pengtu.gsj.utils.MathsUtils;

@Service
@Transactional
public class AutomationEquipmentServiceImpl implements AutomationEquipmentService {

	@Autowired
	private CheckTableService checkTableService;
	@Autowired
	private SystemManageService systemManageService;
	@Autowired
	private DownService downService;
	@Autowired
	private TecconevalutionService tecconevalutionService;
	
	@Override
	public Map<String, Object> getCheckContent(String startTime,
			String endTime, CheckTable checkTable, String tag) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		List<List<String>> totalScoreList = new ArrayList<List<String>>();
		List<Object> values = new ArrayList<Object>();
		List<TecConEvalution> tecConEvalutions = new ArrayList<TecConEvalution>();
		
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		double poitMark = Double.parseDouble((String)jsonObject.get("poitMark"));
		double reserveMark = Double.parseDouble((String)jsonObject.get("reserveMark"));
		double firstStepScore = Double.parseDouble((String)jsonObject.get("firstStepScore"));
		double secondStepScore = Double.parseDouble((String)jsonObject.get("secondStepScore"));
		double thirdStepScore = Double.parseDouble((String)jsonObject.get("thirdStepScore"));
		double total =0; //检查得分
		double percentage =0; //系统完善度
		double tableFull = (yearFull+reserveMark)*4;
		//查询所有相关的附件
		List<Attachment> list = systemManageService.getCheckAttachments(startTime, String.valueOf(Integer.parseInt(endTime)-1), tag);
		
		tecConEvalutions = tecconevalutionService.getCheckData(startTime, String.valueOf(Integer.parseInt(endTime)-1));
		for (TecConEvalution tecConEvalution : tecConEvalutions) {
			double result = 0;
			List<String> resultList = Arrays.asList("0", "0", "0", "0");
			//根据检测比例得到的分数
			if (tecConEvalution.getAutocheckRatio() == 100) {
				result = MathsUtils.sumDouble(result, yearFull);
			} else if (tecConEvalution.getAutocheckRatio() < 90) {
				result = MathsUtils.sumDouble(result, yearPart);
			} else {
				result = MathsUtils.sumDouble(result, poitMark);
			}
			//根据各项报告的有无算分
			for (Attachment attachment : tecConEvalution.getAttachments()) {
				if("zdhsb_lmjcbg".equals(attachment.getTag())) {
					if ("0".equals(resultList.get(0))) {
						result = MathsUtils.sumDouble(result, firstStepScore);
					}
					resultList.set(0, "1");
				}
				if("zdhsb_qljcbg".equals(attachment.getTag())) {
					if ("0".equals(resultList.get(1))) {
						result = MathsUtils.sumDouble(result, secondStepScore);
					}
					resultList.set(1, "1");
				}
				if("zdhsb_sdjcbg".equals(attachment.getTag())) {
					if ("0".equals(resultList.get(2))) {
						result = MathsUtils.sumDouble(result, thirdStepScore);
					}
					resultList.set(2, "1");
				}
				list.add(attachment);
			}
			total = MathsUtils.sumDouble(total, result);
			resultList.set(3, String.valueOf(result));
			totalScoreList.add(resultList);
		}
		percentage = MathsUtils.divDouble(total*100, tableFull, 2);
		//需要更新数据
		values.add("true");
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, values);
		map.put("att", list);
		map.put("score", checkTable.getTestFull());
		map.put("percentage", checkTable.getPerfection());
		map.put("checkTable", checkTable);
		map.put("tecConEvalutions", tecConEvalutions);
		map.put("totalScoreList", totalScoreList);
		return map;
	}

	
}
